﻿using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.StoreApps;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer.ShareTarget;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace NavigationApp.ViewModels
{
    public class NextPageViewModel : ViewModel
    {
        private string message;

        public string Message
        {
            get { return this.message; }
            set { this.SetProperty(ref this.message, value); }
        }

        private BitmapImage sharedImage;

        public BitmapImage SharedImage
        {
            get { return this.sharedImage; }
            set { this.SetProperty(ref this.sharedImage, value); }
        }


        public override async void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            this.Message = navigationParameter + " " + DateTime.Now;

            var shareOperation = navigationParameter as ShareOperation;
            if (shareOperation == null)
            {
                return;
            }

            if (shareOperation.Data.Contains(StandardDataFormats.Bitmap))
            {
                var shareStream = await shareOperation.Data.GetBitmapAsync();
                var bitmapStream = await shareStream.OpenReadAsync();
                var bitmapImage = new BitmapImage();
                bitmapImage.SetSource(bitmapStream);
                this.SharedImage = bitmapImage;
            }
        }
    }
}
